package org.yangspring.samples.mvc.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domain.BooksTPerC;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/19.
 */

@Repository
public class BooksDaoImpl implements BooksDao {

    private static final Logger logger = LoggerFactory.getLogger(BooksDaoImpl.class);

    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryMysql")
    private SessionFactory sessionFactory;

    /*public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/
    /*public SessionFactory getSessionFactory(){
        return this.sessionFactory;
    }*/

    public void addBook(BooksTPerC book){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        logger.warn("\n\n\tBook DAO add\t: " +
                "\tID:\t" + book.getBookId() +
                "\tName:\t" + book.getBookName() +
                "\tPublisher:\t" + book.getBookPublisher() + "\n\n");
        session.persist(book);
        logger.info("Book saved successfully, Book Details=" + book);
    }

    public void updateBook(BooksTPerC book){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        session.update(book);
        logger.info("Book updated successfully, Book Details=" + book);
    }

    public List<BooksTPerC> listBook(){
        logger.warn("\n\nBooks DAO list\n\n");
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        List<BooksTPerC> booksList = session.createQuery("from BooksTPerC").list();
        for(BooksTPerC b : booksList){
            logger.info("\n\n\tBook List::" + b);
        }
        return booksList;

//        return new ArrayList<BooksTPerC>();
    }

    public BooksTPerC getBookById(long id){
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        BooksTPerC b = (BooksTPerC) session.load(BooksTPerC.class, new Long(id));
        logger.info("Book loaded successfully, Book details=" + b);
        return b;
//        return new BooksTPerC();
    }

    public void removeBook(long id){
        logger.warn("\n\nBooks DAO remove\n\n");
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        BooksTPerC b = (BooksTPerC) session.load(BooksTPerC.class, new Long(id));

        if(null != b){
            logger.warn("\n\n\tdelete ?\t" + b.toString());
            session.delete(b);
        }
        logger.info("Book deleted successfully, Book details=" + b);
    }


}
